package grape.composite.service.activity.impl;

import grape.composite.service.activity.api.IActivityContentService;
import grape.composite.service.activity.po.Activity;
import grape.composite.service.activity.mapper.ActivityMapper;
import grape.composite.service.activity.api.IActivityService;
import grape.common.service.common.BaseServiceImpl;
import grape.composite.service.activity.po.ActivityContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author yangwei
 * @since 2020-08-25
 */
@Service
public class ActivityServiceImpl extends BaseServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Autowired
    private IActivityContentService iActivityContentService;

    @Transactional
    @Override
    public Activity insert(Activity entity) {
        Activity activity = super.insert(entity);
        if (activity != null) {
            // 先保存内容
            if (!isStrEmpty(entity.getContent())) {
                ActivityContent activityContent = new ActivityContent();
                activityContent.setActivityId(activity.getId());
                activityContent.setContent(activity.getContent());

                ActivityContent insertContent = iActivityContentService.insert(activityContent);
                if (insertContent == null) {
                    throw new RuntimeException("插入内容错误");
                }
            }
        }

        return activity;
    }
    @Transactional
    @Override
    public boolean removeById(Serializable id) {
        iActivityContentService.removeByActivityId(id.toString());
        return super.removeById(id);
    }
    @Transactional
    @Override
    public boolean updateFullById(Activity entity) {
        if (!isStrEmpty(entity.getContent())) {
            iActivityContentService.updateByActivityId(entity.getId(), entity.getContent());
        }
        return super.updateFullById(entity);
    }
    @Transactional
    @Override
    public boolean updateById(Activity entity) {
        if (!isStrEmpty(entity.getContent())) {
            iActivityContentService.updateByActivityId(entity.getId(), entity.getContent());
        }
        return super.updateById(entity);
    }
}
